import hashlib
import json
from time import time
from urllib.parse import urlparse

import requests


class BlockChain(object):
    def __init__(self):
        self.chain = []
        self.current_transactions = []
        self.nodes = set()
        self.new_block(previous_hash=1, proof=100)

    def new_block(self, proof, previous_hash=None):
        # Creates a new Block and adds it to the chain
        block = {
            'index': len(self.chain) + 1,
            'timestamp': time(),
            'transactions': self.current_transactions,
            'proof': proof,
            'previous_hash': previous_hash or self.hash(self.chain[-1]), }  # Reset the current list of transactions
        self.current_transactions = []
        self.chain.append(block)
        return block

    def new_transaction(self, sender, recipient, amount):
        # Adds a new transaction to the list of transactions
        self.current_transactions.append({'sender': sender, 'recipient': recipient, 'amount': amount, })
        return self.last_block['index'] + 1

    @staticmethod
    def hash(block):
        # Hashes a Block
        block_string = json.dumps(block, sort_keys=True).encode()
        return hashlib.sha256(block_string).hexdigest()

    @property
    def last_block(self):
        # Returns the last Block in the chain
        return self.chain[-1]

    def proof_of_work(self, last_proof):
        """ 简单的工作量证明: - 查找一个 p' 使得 hash(pp') 以4个0开头 - p 是上一个块的证明, p' 是当前的证明 :param last_proof: :return: """
        proof = 0
        while self.valid_proof(last_proof, proof) is False:
            proof += 1
        return proof

    @staticmethod
    def valid_proof(last_proof, proof):
        """ 验证证明: 是否hash(last_proof, proof)以4个0开头? :param last_proof: Previous Proof
        :param last_proof:
        :param proof: Current Proof :return: True if correct, False if not. """
        guess = f'{last_proof}{proof}'.encode()
        guess_hash = hashlib.sha256(guess).hexdigest()
        return guess_hash[:4] == "0000"

    def register_node(self, address):
        """ Add a new node to the list of nodes
        :param address: Address of node. Eg. 'https://192.168.0.5:5000'
        :return: None """
        parsed_url = urlparse(address)
        self.nodes.add(parsed_url.netloc)

    def valid_chain(self, chain):
        """ Determine if a given blockchain is valid :param chain: A blockchain :return: True if valid, False if not """
        last_block = chain[0]
        current_index = 1
        while current_index < len(chain):
            block = chain[current_index]
            print(f'{last_block}')
            print(f'{block}')
            print("\n-----------\n")  # Check that the hash of the block is correct
            if block['previous_hash'] != self.hash(last_block):
                return False  # Check that the Proof of Work is correct
            if not self.valid_proof(last_block['proof'], block['proof']):
                return False
            last_block = block
            current_index += 1
        return True

    def resolve_conflicts(self):
        """ 共识算法解决冲突 使用网络中最长的链. :return: True 如果链被取代, 否则为False """
        neighbours = self.nodes
        new_chain = None  # We're only looking for chains longer than ours
        max_length = len(self.chain)  # Grab and verify the chains from all the nodes in our network
        for node in neighbours:
            response = requests.get(f'https://{node}/chain')
            if response.status_code == 200:
                length = response.json()['length']
                chain = response.json()['chain']  # Check if the length is longer and the chain is valid
                if length > max_length and self.valid_chain(chain):
                    max_length = length
                    new_chain = chain  # Replace our chain if we discovered a new, valid chain longer than ours
                    if new_chain:
                        self.chain = new_chain
                        return True
                    return False

        # block = {'index': 1,
    #          'timestamp': 1506057125.900785,
    #          'transactions': [
    #              {'sender': "8527147fe1f5426f9dd545de4b27ee00",
    #               'recipient': "a77f5cdfa2934df3954a5c7c7da5df1f",
    #               'amount': 5, }],
    #          'proof': 324984774000,
    #          'previous_hash': "2cf24dba5fb0a30e26e83b2ac5b9e29e1b161e5c1fa7425e73043362938b9824"}


class Wallet:
    def __init__(self):
        self._money = 0
        self._primary_key = 123
        self.public_key = 456
        pass
